﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Snowing.Json
{
    [JsonObject]
    public class JsonResult
    {
        public static int DefaultStatus { get; set; }

        [JsonProperty(PropertyName = "status", Order = 0)]
        public int Status { get; set; }

        [JsonProperty(PropertyName = "message", Order = 1)]
        public string Message { get; set; }

        public bool IsSuccess()
        {
            return this.Status == DefaultStatus;
        }

        public JsonResult Fail(int status, string message)
        {
            this.Status = status;
            this.Message = message;
            return this;
        }

        public JsonResult()
        {
            this.Status = DefaultStatus;
            this.Message = "OK";
        }
    }

    [JsonObject]
    public class JsonResult<T> : JsonResult
    {
        [JsonProperty(PropertyName = "data", Order = 2)]
        public T Data { get; set; }

        public JsonResult()
        {
            this.Data = default(T);
            this.Message = "OK";
        }
    }

    [JsonObject]
    public class CollectionResult<T>
    {
        [JsonProperty("items")]
        public IList<T> Items { get; set; }

        public CollectionResult()
        {
            this.Items = new List<T>();
        }
    }

    /// <summary>
    /// 封装一层集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class JsonListObjectResult<T> : JsonResult<CollectionResult<T>>
    {
        public JsonListObjectResult()
        {
            this.Data = new CollectionResult<T>();
        }
    }

    /// <summary>
    /// 直接集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class JsonCollectionResult<T>: JsonResult<IList<T>>
    {
        public JsonCollectionResult()
        {
            this.Data = new List<T>();
        }
    }
}
